package com.jinzecloud.fiction.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jinzecloud.fiction.vo.TbCarteGetNameAndIdVo;
import com.jinzecloud.fiction.vo.TbCarteVo;
import com.sun.xml.bind.v2.TODO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinzecloud.fiction.utils.PageUtils;
import com.jinzecloud.fiction.utils.Query;

import com.jinzecloud.fiction.mapper.TbCarteMapper;
import com.jinzecloud.fiction.bean.TbCarte;
import com.jinzecloud.fiction.service.TbCarteService;


@Service("tbCarteService")
public class TbCarteServiceImpl extends ServiceImpl<TbCarteMapper, TbCarte> implements TbCarteService {

    @Autowired
    TbCarteMapper tbCarteMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        // TODO 条件查询
        QueryWrapper<TbCarte> tbCarteQueryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){

        }
        IPage<TbCarte> page = this.page(
                new Query<TbCarte>().getPage(params),
                new QueryWrapper<TbCarte>()
        );

        return new PageUtils(page);
    }

    @Override
    public List getList(Map<String, Object> params) {
        QueryWrapper<TbCarte> tbCarteQueryWrapper = new QueryWrapper<>();
        List<TbCarte> tbCartes = new ArrayList<>();
        // TODO 查询条件,查询的数据需要带着父级和子级
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            tbCarteQueryWrapper.eq("ca_id",key);
        }

        if (!StringUtils.isEmpty(key)){
            tbCarteQueryWrapper.or().like("ca_name",key);
        }
        // 查出所有分类数据
        if(!StringUtils.isEmpty(key)){
            tbCartes = tbCarteMapper.selectList(tbCarteQueryWrapper);
        }else {
            tbCartes = tbCarteMapper.selectList(new LambdaQueryWrapper<TbCarte>().orderByAsc(TbCarte::getCaParent));
        }
        Map<Integer, TbCarteVo> tbCarteVoMap = getIntegerTbCarteVoMap(tbCartes);


        return List.copyOf(tbCarteVoMap.values());
//
    }

    @Override
    public List getCarteNameAndId() {
        List<TbCarte> tbCartes = tbCarteMapper.selectList(null);
        List<TbCarteGetNameAndIdVo> tbCarteVos = tbCartes.stream().map(tbCarte -> {
            TbCarteGetNameAndIdVo tbCarteGetNameAndIdVo = new TbCarteGetNameAndIdVo();
            BeanUtils.copyProperties(tbCarte, tbCarteGetNameAndIdVo);
            return tbCarteGetNameAndIdVo;
        }).collect(Collectors.toList());

        List<Object> list = tbCarteVos.stream().filter(tbCarteGetNameAndIdVo -> {
            return tbCarteGetNameAndIdVo.getCaParent() == 0;
        }).map(tbCarteGetNameAndIdVo -> {
            tbCarteGetNameAndIdVo.setChildren(getChildren(tbCarteGetNameAndIdVo,tbCarteVos));
            return tbCarteGetNameAndIdVo;
        }).collect(Collectors.toList());
        return list;
    }

    @Override
    public void saveCarte(TbCarte tbCarte) {
        tbCarteMapper.insert(tbCarte);
    }

    private static Map<Integer, TbCarteVo> getIntegerTbCarteVoMap(List<TbCarte> tbCartes) {
        // 将查出的数据拷贝给Vo类
        List<TbCarteVo> tbCarteVos = tbCartes.stream().map(tbCarte -> {
            TbCarteVo tbCarteVo = new TbCarteVo();
            BeanUtils.copyProperties(tbCarte, tbCarteVo);
            return tbCarteVo;
        }).collect(Collectors.toList());

        Map<Integer, TbCarteVo> idMap = tbCarteVos.stream().collect(Collectors.toMap(TbCarteVo::getCaId, Function.identity()));
        Map<Integer, TbCarteVo> tbCarteVoMap = new TreeMap<>();
        idMap.forEach((k, v) -> {
            TbCarteVo parentVal = idMap.get(v.getCaParent());
            if (parentVal == null) {
                tbCarteVoMap.put(k, v);
                return;
            }
            parentVal.addChildren(v);
        });
        return tbCarteVoMap;
    }


    private List<TbCarteGetNameAndIdVo> getChildren(TbCarteGetNameAndIdVo tbCarteGetNameAndIdVo, List<TbCarteGetNameAndIdVo> tbCarteVos) {
        List<TbCarteGetNameAndIdVo> list = tbCarteVos.stream().filter(tbCarteVo1 ->
                    tbCarteVo1.getCaParent() == tbCarteGetNameAndIdVo.getCaId()
                )
                .map(tbCarteVo1 -> {
                    tbCarteVo1.setChildren(getChildren(tbCarteVo1, tbCarteVos));
                    return tbCarteVo1;
                }).collect(Collectors.toList());
        return list;
    }

}